﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace AtTheGatesCommon.Utilities
{
    public class EnumerationDictionary<T> : IDictionary<string, T>
        where T : IComparable, IFormattable, IConvertible
    {
        private Dictionary<string, T> _SortedEnumeration;

        public EnumerationDictionary()
        {
            var enumType = typeof(T);
            var values = Enum.GetValues(enumType);
            var names = Enum.GetNames(enumType);
            _SortedEnumeration = new Dictionary<string, T>(names.Length);

            for (int i = 0; i < names.Length; i++)
                _SortedEnumeration.Add(names[i], (T)values.GetValue(i));
        }

        #region IDictionary Implementation

        public bool IsReadOnly { get { return true; } }

        public void Add(string key, T value) { throw new InvalidOperationException("This collection is read-only"); }
        public void Add(KeyValuePair<string, T> item) { throw new InvalidOperationException("This collection is read-only"); }
        public bool Remove(string key) { throw new InvalidOperationException("This collection is read-only"); }
        public bool Remove(KeyValuePair<string, T> item) { throw new InvalidOperationException("This collection is read-only"); }
        public void Clear() { throw new InvalidOperationException("This collection is read-only"); }

        public bool ContainsKey(string key) { return _SortedEnumeration.ContainsKey(key); }
        public ICollection<string> Keys { get { return _SortedEnumeration.Keys; } }
        public bool TryGetValue(string key, out T value) { return _SortedEnumeration.TryGetValue(key, out value); }
        public ICollection<T> Values { get { return _SortedEnumeration.Values; } }
        public int Count { get { return _SortedEnumeration.Count; } }
        public bool Contains(KeyValuePair<string, T> item) { return _SortedEnumeration.Contains(item); }
        public void CopyTo(KeyValuePair<string, T>[] array, int arrayIndex) { ((IDictionary<string, T>)_SortedEnumeration).CopyTo(array, arrayIndex); }
        public IEnumerator<KeyValuePair<string, T>> GetEnumerator() { return _SortedEnumeration.GetEnumerator(); }
        IEnumerator IEnumerable.GetEnumerator() { return _SortedEnumeration.GetEnumerator(); }

        public T this[string key]
        {
            get { return _SortedEnumeration[key]; }
            set { throw new InvalidOperationException("This collection is read-only"); }
        }

        #endregion

    }
}
